package org.javalearn.javalearn;

//集合及泛型
import java.util.Collections;

public interface Collection{
    boolean add(Object o);
    remove(Object o);
    int size();
    boolean contains(Object o);
}


/**
 *
 * @author z
 */

public class Javalearn {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

/*
public class A{

    public static void main(String[] args){
        Student[] sa = new Student[135];

        for(int i = 0;i < sa.length;i++){//java不允许下标越界
            sa[i] = new Student();
        }

        int[][] a = new int[2][3]; //每行3个对象  a[0]和a[1]分别指向 两个int[]类型的对象 每个int[]对象有3个对象
        //a[1] = new int[5]; a[1]原来指向的变成垃圾对象
        //a[1]指向5个对象  
        a[1] = new int[5];
        for(int i = 0;i<a.length;i++){
            for (int j=0;j<a[i].length;j++){
                a[i][j]=0;
            }
        }

        Student s1 = null;
        Student s2 = 


    }

}

//猜数游戏 1-100
public class NumguessBean{
    int numGuesses;//猜的次数
    int answer;//答案
    String hint;//提示

    boolean success;//是否答对

    public void reset(){
        success = false;
        numGuesses = 0;

        answer = (int)(Math.random()*100+1);
        answer = new Random().nextInt()%100+1;

    }

    public void setGuess(String guess){
        numGuesses++;
        int g = -1;
        try{
            g = Integer.valueOf(guess);
            g = Integer.parseInt(guess);
        }
        catch(Exception e){

        }
        if(g==-1){
            hint = "a number please";
            return;
        }

        if(g==answer){
            success = true;
        }
        else(if(g<answer)){
            hint = "higher";
        }
        else{
            hint = "lower";
        }

    }

}

System.in;
System.out;

*/

public class Mobile extends Object{ //没写父类 默认继承Object

    public static void main(String[] args){ //一个引用变量可以引用该类型的实例或该类型的派生类型的实例

        Mobile m;

        IPhone16 iphone = null;


        m = iphone;

        iphone = m;

        Object o1 = new Mobile();
        Object o2 = new IPhone16();
        Object o3 = new int[0];
        Object o3 = new Student[133];
        ((Mobile)o1).dial("18640857288")

        if(o1 instanceof Fruit){ //instanceof判断某个变量是否为某个类型数据的实例
            Fruit f = (Fruit) o1;
        }
    }

}

class IPhone16 extends Mobile{//类单一继承，父类唯一   ，类继承描述 is  a  父类基数多 

//子类型可以给父类型赋值，提升

}

abstract class X{
    abstract public void a();//有抽象函数时，类必须声明为抽象类，不能new抽象类
}

abstract class Shpae{
    abstract public double area();//抽象函数没函数体
    public abstract double fun();
}

class Edge{
    public static void a(){} 
}

//接口实现
public interface K extends K1,K2{ //接口可以多重继承
    //接口是定义规划，不能有实现
    int i = 0;
    void a();
}


public class A implements K{ //抽象类可以实现部分继承的实现，其他的继承实现可以在派生类实现
    public void a(){}
}

public class B implements K{
    public void a(){

    }
}
//接口使用
public class Test{
    public static void main(String[] args) {
        K k1 = new A();
        K k2 = new B();
        k1.a();
        k2.a();
    }
}

//
public class NumguessGame{

    public NumguessBean bean = new NumguessBean();
    int[] history = new int[100];
    int hindex = 0;

    public void setGuess(String guess){
        NumGuesses++;
        int g = -1;
        try {
            g = Integer.valueOf(guess);
        } catch (Exception e) {

        }
        if(g==-1){

        }

        history[hindex++]=g;
    }
    public void showInput(){
        System.out.print("(");
        for(int i = 0;i < hindex;i++){
            System.out.println(history[i]+",");
        }
        System.out.println(")");
        System.out.println("what's your guess?");
    }
    
}

//泛型
public class Student {
    public String id,name;
    int age;
    boolean gender;
    //元类，Class meat-class 描述类的类
    //根类，Object
    //Java反射机制

    public boolean equals(Object o){
        if(o instanceof Student){
            Object,getClass()
            if(o.getClass() == Student.class){
            Studnet s = (Student) o;
            return this.id.equals(s.id)&&
            this.name.equals(s.name)&&
            this.age == s.age&&
            this.gender == s.gender;
            }
        }

        return this == o;
    }

    Student s1 = new Student();
    Student s2 = new Student();

    Class clazz = s.getClass();//虚拟机可以创建Class类对象

    Student.class == clazz;
    Sutdent.class == s.getClass();
}


public class Student{
    public boolean equals(Object o){
        if(o.getClass() == Sutdent.class){
            Student.s = (Student)o;
            return s.id.equals(this.id);
        }
        else{
            return false;
        }
    }
}
//集合
//equals()函数
Collections c = new ArrayList();
Collections c = new LinkedList();

c.add(new Student());
c.add(new Apple());
c.add(new String("abc"));

boolean r = c.contains("abc");

c.remove(new String("abc"));
//此时c.size() = 2

List list = new ArrayList();
List list = new LinkedList();
List list = new Vector();

list.add(new String("abc"));
list.add(new Integer(5));
list.add(new Double(5));

list.get(0);

list.remove(1);
list.set(2,new Integer(5));

public class R{
    public static void main(String[] args) {
        List c = new ArrayList();
        for(int i = 1;i<=35;i++){
            c.add(new Integer(i));
        }

        List s = new ArrayList();
        for(int i = 0;i < 7;i++){
            Collections.shuffle(c);
            s.add(c.remove(0));
        }

        Collections.sort(s);

        for(int i = 0;i < 7;i++){
            if(i!=6){
            System.out.print(s.get(i) + ",");
            }
            else{
            System.out.print(s.get(i));
            }
        }
    }

}

Set,Map

Set set = new HashSet();
Set set = new TreeSet();

Map map = new HashMap();
map.put("123",new Student());
map.put(new String("123"),new Integer(5));
map.size() //返回1
map.get("123")//Integer(5)覆盖了前面的123

List list = new ArrayList();
list.add("abc");
list.add(0,new Student());
//无法强制转换student为字符串
String s = (String)list.get(0);

//泛型 (类似类模板)
List<String> list = new ArrayList<String>();

list.add(new Student);//报错

String s = list.get(0);
//自定义泛型
class A <String>{}

Map<String,Object> map = new HashMap<String,Object>();
Map<String,?>x;
public void a(Map<String,?>map){

}
Map<String,? extends Student>x;//接收学生及其派生类 Student上限

Map<String,? super Student>x;//接收学生及其父类 下限

//封装，多态，继承
public class Student {
    private String id,name;
    private int age;

    public int getAge(){
        return age;
    }
    public void setAge(int age){
        if(age<0){
            return;
        }
        this.age = age;
    }
}
//多态 行为具有多样性
//重载
public class Cal{
    public int add(int i,int j){}
    public double add(double i,double j){}
}
//覆盖
public class BadStudent extends Student{
    public void study(){
        travel();
    }
}
//关键字super父类  this当前对象
public class A {
    void a(){}
}
public class B extends A {
    public void a(){
        super.a();//访问父类的a()函数 父类没有 访问父类的父类的a()函数
        this.b();//可能调用派生类或当前的b函数
    }
    public void b(){
        
    }
    public static void c(){
        this.a();
    }
}
//this 不能出现在static函数中
/*在static函数中不能使用this关键字，因为static方法不需要创建对象，
*所以没有当前对象。在调用static方法时，不需要“当前对象”参与，
*因此不能使用this关键字。同时，static域不属于this指向的范围所在，所以也不能调用。
*/

//构造函数
class A{
    private int i;
    public A(int i){//写了构造函数，编译器不在提供默认的
        this.i = i;
        return;
    }
    public A(){this.i = 0;return;}
    public static void main(String[] args) {
        A a = new A();//没有构造函数时，编译器提供一个默认的无参数的构造函数，和类的访问权限一致
    }
    
}

public class A{
    private int i;
    public A (int i){
        this.i = i;
    }
}
public class B extends A{
    private String s;
    public B(String s,int i){//父类先初始化，子类再初始化
        //第一条不是this，super时，系统自动加一个super()无参 对父类的构造函数;
        super(i);//调用父类初始化
        //this.i = i;//非法，父类的私有成员不能访问
        this.s = s;
    }
    public static void main(String[] args){
        A a = new B("hello");
    }
}

//输出3 5
class A{
    int i = 2;
    A(){
        a();
    }
    public static void main(String[] args) {
        System.out.println(i);
    }
}

class B extends A{
    B(int i){
        this.i = i;
    }
    public void a(){
        System.out.println(i+1);
    }
    public static void main(String[] args) {
        System.out.println(new B(5).i);
    }
}

class Point{
    private int x,y;

    public Point(int x,int y){
        this.x = x;
        this.y = y;
    }

    public Point(){
        this();//递归调用Point() ，又调用this()，无限递归
        //最终 JVM 抛出 StackOverflowError，因为 方法调用栈溢出。
    }
}

//包
//C++中
// namespace a{
//     class A{};
// }
// namespace b{
//     class A{};
// }

// a::A * pa = new a::A();

// b::A * pb = new b::A();

//java中
package b;//package b.c.d;
class A{
   public A();
}

package com.neusoft.tele.customer;
 
class PositiveStudent{//外部类  只能是public 或不加修饰符(为default)
    public class A{//内部类
        
    }
    int age;//没加修饰符为default  包私有，同一个包内的可以用
}

//private  (default)  protected  public
package a;

public class A{
    protected void a(){}
    //protected 同包能访问
}

public class B extends a.A{//子类能访问protected
    void b(){
        a();
        a.A a = new a.A();
        a.a();
    }
}

import a.A;
public class C extends A{//子类能访问protected
    void b(){
        a();
        A a = new C();//子类继承父类，只能通过子类的实例访问protected，不能通过父类的实例访问
        a.a();        //子类继承父类，只能通过子类的实例访问protected，不能通过父类的实例访问
    }

    void c(){
        a();
        C c = new C();
        c.a();
    }
}

import java.util.Set;
import java.util.*;

class A{
    final void a(){}//final不允许被重写
}

package c;

public class Calc{//公开类保存为文件时必须与类名一致 去掉public则随便，但是一般一个文件只有一个类
    public int add(int i,int j){
        return i+j;
    }
    public int subtract(int i,int j){
        return i - j - 1;
    }
}












































































































